import React, { Component } from 'react';
import Video from 'react-native-video';
import WebView from 'react-native-webview';
import Orientation from 'react-native-orientation';
import { getShareNewsHtml } from '~/api/share';
import { Flex, Toast, WingBlank } from '@ant-design/react-native';
import { Image, ScrollView, StyleSheet, Text, View } from 'react-native';
import { connect } from 'react-redux';

const mapGetter = (state) => {
    return {
        userinfo: state.userinfo,
    };
};
class HVideo extends Component {
    static navigationOptions = ({ navigation }) => {
        let is_portrait = true;
        let title = '视频';
        if (navigation.state.params) {
            if (navigation.state.params.isLandscape) {
                is_portrait = !navigation.state.params.isLandscape;
            }
            if (navigation.state.params.title) {
                title = navigation.state.params.title;
            }
        }
        return {
            title: title,
            headerShown: is_portrait,
        };
    };

    constructor(props) {
        super(props);
        this.state = {
            id: this.props.navigation.getParam('id') || 0,
            link: this.props.navigation.getParam('link') || '',
            title: this.props.navigation.getParam('title') || '',
            cover: this.props.navigation.getParam('cover') || '',
            html: '',
        };
        this.props.navigation.setParams({ title: this.state.title });
        if (this.state.link === '') {
            getShareNewsHtml({
                id: this.state.id,
            }).then((r) => {
                if (r.code === 0) {
                    let html_new = r.data.html.replace(/<img[^>]*src="([^"]+)"[^>]*>/gi, '<img src="$1">');
                    this.setState({
                        html: html_new,
                    });
                } else {
                    Toast.info('失败', 1);
                }
            });
        }
    }

    componentDidMount() {
        Orientation.getInitialOrientation();
        Orientation.addOrientationListener(this._orientationDidChange);
    }

    _orientationDidChange = (orientation) => {
        if (orientation === 'LANDSCAPE') {
            this.props.navigation.setParams({ isLandscape: true });
        } else {
            this.props.navigation.setParams({ isLandscape: false });
        }
    };

    componentWillUnmount() {
        Orientation.removeOrientationListener(this._orientationDidChange);
    }

    render() {
        return (
            <ScrollView style={{ flex: 1 }}>
                <Flex style={{ width: '100%', height: 200 }}>
                    {this.state.link !== '' && (
                        <Video
                            source={{ uri: this.state.link }}
                            poster={this.state.cover}
                            paused
                            controls
                            style={{ width: '100%', height: '100%' }}
                            resizeMode="contain"
                        />
                    )}
                    {this.state.html !== '' && <WebView source={{ html: this.state.html }} style={{ width: '100%', height: '100%' }} />}
                </Flex>
                <View style={{ backgroundColor: '#FFFFFF', marginTop: 10, padding: 15 }}>
                    <Flex>
                        {this.props.userinfo.identity === 8
                            ?
                            <Text>
                                中国免费车位快销模式共享平台-大卖车位，将卖车位变送车位，业主零首付、拿好礼、返现金、送汽车、得车位，颠覆传统，打破常规，求变创新。
                                大卖车位5.0全流程线上营销体系将车位搬到线上销售，业主随时了解小区车位促销信息，24小时在线下定。
                            </Text>
                            : (this.props.userinfo.identity === 11 || this.props.userinfo.identity === 31
                                ?
                                <Text>中国免费车位快销模式共享平台-大卖车位，将卖车位变送车位，业主零首付、拿好礼、返现金、送汽车、得车位，颠覆传统，打破常规，求变创新。
                                平台秉承“让天下没有难卖房产”使命和“争创房地产创新营销第一品牌，赋能全国代理商做到区域市场第一”的愿景，通过整合全国优质资源，为房地产开发商解决存量资产快速变现问题。
                                大卖车位5.0全流程线上营销体系将车位搬到线上销售，开发商24小时线上自动销售车位；银行免费入驻接单、线上审核；业主随时了解小区车位促销信息，24小时在线下定；代理商直接为开发商和业主提供服务。
                            </Text>
                                :
                                <Text>
                                    中国免费车位快销模式共享平台-大卖车位，将卖车位变送车位，业主零首付、拿好礼、返现金、送汽车、得车位，颠覆传统，打破常规，求变创新。
                                    大卖车位5.0全流程线上营销体系将车位搬到线上销售，银行免费入驻，线上审核业主贷款申请材料，线下面签。
                                </Text>
                            )
                        }
                    </Flex>
                </View>
            </ScrollView>
        );
    }
}

var styles = StyleSheet.create({
    backgroundVideo: {
        position: 'absolute',
        top: 0,
        left: 0,
        bottom: 0,
        right: 0,
    },
});
export default connect(mapGetter)(HVideo);